//
//  BasicConfigurationHelper.m
//  XTCloud
//
//  Created by QA on 2017/11/27.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "BasicConfigurationHelper.h"
#import "FilePathHelper.h"

#define kBasicConfigurationKey @"BasicConfigurationKey"
#define kBasicConfigurationFileName @"BasicConfiguration"

@implementation BasicConfigurationHelper

/******存储配置信息*********/
+ (BOOL) saveConfigurationData:(BasicConfigurationCodeing*)userInfo
{
    [self delectConfigurationData];
    
    NSString *Path = [FilePathHelper getUserInfoPath];
    NSString *filePath = [Path stringByAppendingPathComponent:kBasicConfigurationFileName];
    
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *vdArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [vdArchiver encodeObject:userInfo forKey:kBasicConfigurationKey];
    [vdArchiver finishEncoding];
    
    BOOL isSaveSuccess = [data writeToFile:filePath atomically:YES];
    NSLog(@"配置信息保存：%@",isSaveSuccess ? @"成功" : @"失败");
    return isSaveSuccess;
}

/******删除配置信息*********/
+ (void) delectConfigurationData
{
    NSString *Path = [FilePathHelper getUserInfoPath];
    NSString *filename = [Path stringByAppendingPathComponent:kBasicConfigurationFileName ];
    
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager isDeletableFileAtPath:filename]) {
        [defaultManager removeItemAtPath:filename error:nil];
    }
}

/******获取配置信息*********/
+ (BasicConfigurationCodeing*) getConfigurationData
{
    NSString *Path = [FilePathHelper getUserInfoPath];
    NSString *filePath = [Path stringByAppendingPathComponent:kBasicConfigurationFileName];
    
    NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
    NSKeyedUnarchiver *vdUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    BasicConfigurationCodeing *model  = [vdUnarchiver decodeObjectForKey:kBasicConfigurationKey];
    [vdUnarchiver finishDecoding];
    
    return model;
}

@end
